home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991 …esperately Seeking Seven / Desperately Seeking Seven.2mg / Dev.CD.8 / Essentials / Tools / DTS.Samples / SC24Teach / Teach.c / UWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-25  |  13.2 KB  |  473 lines  |  [04] ASCII Text (0x0000)

  1. /***********************************************************************
  2. *
  3. * teach uwindow.c -- Version 3.0 
  4. *
  5. * Copyright (c)
  6. * Apple Computer, Inc.  1986-1990
  7. * All Rights Reserved.
  8. *
  9. * Developer Technical Support Apple II Sample Code
  10. *
  11. * This file contains the code which implements  
  12. * windows in the Teach program.
  13. *
  14. ***********************************************************************/
  15.  
  16. #include <types.h>
  17. #include <gsos.h>
  18. #include <memory.h>
  19. #include <stdfile.h>
  20. #include <quickdraw.h>
  21. #include <textedit.h>
  22. #include <window.h>
  23. #include "teach.h"
  24.  
  25. extern unsigned int     userID, staggerCount;
  26. extern char             test[80];
  27.  
  28.  
  29.  
  30. /***********************************************************************
  31. *
  32. * smartGetWTitle
  33. *
  34. * This function returns a pointer to the window title pascal string.
  35. * This is a little more complicated than it used to be, given that
  36. * NewWindow2 clones the string for the window into an unlocked handle.
  37. * GetWTitle returns a handle with the hi-bit set (to distinguish it
  38. * from a reguler pointer).  If the hi-bit is set, then we want to
  39. * dereference the handle to a pointer, and return that instead.
  40. *
  41. ***********************************************************************/
  42. char            *smartGetWTitle(wptr)
  43. GrafPortPtr     wptr;
  44. {
  45.     char    *wtitle;
  46.  
  47.     wtitle = GetWTitle(wptr);
  48.     if ((long)wtitle & 0x80000000L) wtitle = *(char **)((long)wtitle & 0x7FFFFFFFL);
  49.     return(wtitle);
  50. }
  51.  
  52.  
  53.  
  54. /************************************************************************
  55. *
  56. * drawThisWindow
  57. *
  58. * This routine draws the contents of all the windows.
  59. *
  60. ************************************************************************/
  61. pascal void drawThisWindow()
  62. {
  63.     DrawControls(GetPort());
  64. }
  65.  
  66.  
  67.  
  68. /************************************************************************
  69. *
  70. * doCloseTop
  71. *
  72. * This routine closes the topmost window.
  73. *
  74. ************************************************************************/
  75. void    doCloseTop()
  76. {
  77.     unsigned long   refCon;
  78.  
  79.     if (refCon = GetWRefCon(FrontWindow())) DisposeHandle(refCon);
  80.         /* Get rid of the path name handle that we keep in the refcon. */
  81.         /* Note that it is zero if the window is untitled. */
  82.     
  83.     CloseWindow(FrontWindow());
  84. }
  85.  
  86.  
  87.  
  88. /************************************************************************
  89. *
  90. * PutFileIntoWindow
  91. *
  92. * This routine opens the specified file, reads its contents in and sets
  93. * the text of the text edit control of the top window to that text.
  94. *
  95. ************************************************************************/
  96. void    putFileIntoWindow(c1hndl)
  97. char    **c1hndl;
  98. {
  99.     GSString255     *c1ptr;
  100.     OpenRecGS       openBlock;
  101.     RefNumRecGS     closeBlock;
  102.     IORecGS         readBlock;
  103.     Handle          theHndl;
  104.     unsigned int    temp;
  105.  
  106.     HLock(c1hndl);
  107.     c1ptr = (GSString255 *)(*c1hndl + 2);
  108.  
  109.     openBlock.pCount = 15;              /* Open the indicated file. */
  110.     openBlock.pathname = c1ptr;
  111.     openBlock.requestAccess = 0;
  112.     openBlock.resourceNumber = 0;
  113.     openBlock.optionList = NULL;
  114.  
  115.     OpenGS(&openBlock);                 /* Error -- give it up. */
  116.     if (_toolErr) {
  117.         ErrorWindow(0, NULL, _toolErr);
  118.         HUnlock(c1hndl);
  119.         return;
  120.     }
  121.  
  122.     closeBlock.pCount = 1;              /* Get ready to close file. */
  123.     closeBlock.refNum = openBlock.refNum;
  124.  
  125.     theHndl = NewHandle(openBlock.eof, userID, 0, NULL);    /* Make a handle for file. */
  126.     if (_toolErr) {
  127.         ErrorWindow(0, NULL, _toolErr);
  128.         CloseGS(&closeBlock);
  129.         HUnlock(c1hndl);
  130.         return;
  131.     }
  132.     HLock(theHndl);
  133.  
  134.     readBlock.pCount = 4;               /* Read the file. */
  135.     readBlock.refNum = openBlock.refNum;
  136.     readBlock.dataBuffer = *theHndl;
  137.     readBlock.requestCount = openBlock.eof;
  138.     ReadGS(&readBlock);
  139.  
  140.     if (_toolErr) {                     /* Error -- give it up. */
  141.         ErrorWindow(0, NULL, _toolErr);
  142.         CloseGS(&closeBlock);
  143.         DisposeHandle(theHndl);
  144.         HUnlock(c1hndl);
  145.         return;
  146.     }
  147.  
  148.     CloseGS(&closeBlock);               /* Almost made it -- oh well. */
  149.     if (_toolErr) {
  150.         ErrorWindow(0, NULL, _toolErr);
  151.         DisposeHandle(theHndl);
  152.         HUnlock(c1hndl);
  153.         return;
  154.     }
  155.  
  156.     TESetText(
  157.         teDataIsTextBlock+refIsHandle*8,                    /* Text Descriptor */
  158.         theHndl,                                            /* Text Ref */
  159.         0L,                                                 /* Text Length */
  160.         0,                                                  /* Style Descriptor */
  161.         NULL,                                               /* Style Ref */
  162.         GetCtlHandleFromID(FrontWindow(),MainWindowID)      /* Control Handle */
  163.     );
  164.  
  165.     DisposeHandle(theHndl);
  166.     HUnlock(c1hndl);
  167. }
  168.  
  169.  
  170.  
  171. /************************************************************************
  172. *
  173. * putWindowIntoFile
  174. *
  175. * This routine opens the specified file, writes the window contents.
  176. *
  177. ************************************************************************/
  178. void    putWindowIntoFile(c1hndl)
  179. char    **c1hndl;
  180. {
  181.     GSString255     *c1ptr;
  182.     NameRecGS       destroyBlock;
  183.     CreateRecGS     createBlock;
  184.     OpenRecGS       openBlock;
  185.     RefNumRecGS     closeBlock;
  186.     IORecGS         writeBlock;
  187.     Handle          theHndl;
  188.     unsigned int    temp, err;
  189.     unsigned long   totalSize;
  190.  
  191.     HLock(c1hndl);
  192.     c1ptr = (GSString255 *)(*c1hndl + 2);
  193.  
  194.     destroyBlock.pCount = 1;
  195.     destroyBlock.pathname = c1ptr;
  196.     DestroyGS(&destroyBlock);           /* May or may not be there. */
  197.     if ((_toolErr) && (_toolErr != fileNotFound)) {
  198.         ErrorWindow(0, NULL, _toolErr);
  199.         HUnlock(c1hndl);
  200.         return;
  201.     }
  202.  
  203.     createBlock.pCount = 4;             /* Now it isn't there for sure. */
  204.     createBlock.pathname = c1ptr;
  205.     createBlock.access = 0xC3;
  206.     createBlock.fileType = 0x04;
  207.     createBlock.auxType = 0;
  208.     CreateGS(&createBlock);
  209.     if (_toolErr) {
  210.         ErrorWindow(0, NULL, _toolErr);
  211.         HUnlock(c1hndl);
  212.         return;
  213.     }
  214.  
  215.     openBlock.pCount = 15;
  216.     openBlock.pathname = c1ptr;
  217.     openBlock.requestAccess = 0;
  218.     openBlock.resourceNumber = 0;
  219.     openBlock.optionList = NULL;
  220.     OpenGS(&openBlock);
  221.     if (err = _toolErr) {
  222.         DestroyGS(&destroyBlock);
  223.         ErrorWindow(0, NULL, err);
  224.         HUnlock(c1hndl);
  225.         return;
  226.     }
  227.  
  228.     closeBlock.pCount = 1;
  229.     closeBlock.refNum = openBlock.refNum;
  230.  
  231.     theHndl = NewHandle(1L, userID, 0, NULL);
  232.     if (!_toolErr) {
  233.         totalSize = TEGetText(
  234.             teDataIsTextBlock+refIsHandle*8,                    /* Text Descriptor */
  235.             theHndl,                                            /* Text Ref */
  236.             0L,                                                 /* Text Length */
  237.             0,                                                  /* Style Descriptor */
  238.             0L,                                                 /* Style Ref */
  239.             GetCtlHandleFromID(FrontWindow(), MainWindowID)     /* Control Handle */
  240.         );
  241.     }
  242.     else theHndl = NULL;
  243.     if (err = _toolErr) {
  244.         if (theHndl) DisposeHandle(theHndl);
  245.         CloseGS(&closeBlock);
  246.         DestroyGS(&destroyBlock);
  247.         ErrorWindow(0, NULL, err);
  248.         HUnlock(c1hndl);
  249.         return;
  250.     }
  251.  
  252.     HLock(theHndl);
  253.     writeBlock.pCount = 4;
  254.     writeBlock.refNum = openBlock.refNum;
  255.     writeBlock.dataBuffer = *theHndl;
  256.     writeBlock.requestCount = totalSize;
  257.     WriteGS(&writeBlock);
  258.     err = _toolErr;
  259.     DisposeHandle(theHndl);
  260.     if (err) {
  261.         CloseGS(&closeBlock);
  262.         DestroyGS(&destroyBlock);
  263.         ErrorWindow(0, NULL, err);
  264.         HUnlock(c1hndl);
  265.         return;
  266.     }
  267.  
  268.     CloseGS(&closeBlock);
  269.     if (err = _toolErr) {
  270.         DestroyGS(&destroyBlock);
  271.         ErrorWindow(0, NULL, err);
  272.     }
  273.  
  274.     HUnlock(c1hndl);
  275. }
  276.  
  277.  
  278.  
  279. /************************************************************************
  280. *
  281. * placeAndShowWindow
  282. *
  283. * This routine moves the specified window based on stagger count
  284. * and shows it.
  285. *
  286. ************************************************************************/
  287. void            placeAndShowWindow(theWindow)
  288. GrafPortPtr     theWindow;
  289. {
  290.     MoveWindow(8 + 8 * staggerCount, 28 + 8 * staggerCount, theWindow);
  291.     staggerCount++;
  292.     staggerCount &= 0x07;
  293.     ShowWindow(theWindow);
  294.     SelectWindow(theWindow);
  295. }
  296.  
  297.  
  298.     
  299. /******************************************************************************
  300. *
  301. * doOpenWindow
  302. *
  303. * This routine either asks the user what file to open and opens it.
  304. *
  305. ******************************************************************************/
  306. void    doOpenWindow()
  307. {
  308.     SFReplyRec2     myReply;
  309.     GrafPortPtr     wptr;
  310.     char            *pstr;
  311.  
  312.     SFAllCaps(1);
  313.  
  314.     myReply.nameRefDesc = refIsNewHandle;
  315.     myReply.pathRefDesc = refIsNewHandle;
  316.     SFGetFile2(
  317.         10, 35,
  318.         refIsPointer,
  319.         "\pPick a file, any file.",
  320.         NULL,           /* filter proc */
  321.         NULL,           /* type list   */
  322.         &myReply
  323.     );
  324.  
  325.     if (myReply.good) {
  326.         pstr = *(char **)myReply.nameRef;       /* Convert C1Output string to pascal string. */
  327.         pstr[3] = pstr[2];
  328.         pstr += 3;
  329.  
  330.         HLock(myReply.nameRef);
  331.  
  332.         wptr = NewWindow2(
  333.             pstr,                                       /* Title reference */
  334.             myReply.pathRef,                            /* Ref con */
  335.             drawThisWindow,                             /* Draw routine */
  336.             NULL,                                       /* DefProc pointer */
  337.             refIsResource,                              /* Param Table Descriptor */
  338.             MainWindowID,                               /* Param Table Reference */
  339.             rWindParam1                                 /* Param Table Type */
  340.         );
  341.  
  342.         DisposeHandle(myReply.nameRef);
  343.  
  344.         placeAndShowWindow(wptr);
  345.         putFileIntoWindow(myReply.pathRef);
  346.     }
  347. }
  348.  
  349.  
  350.  
  351. /******************************************************************************
  352. *
  353. * doSaveAs
  354. *
  355. * This routine either saves the file  in the place indicated by the user.
  356. *
  357. ******************************************************************************/
  358. void    doSaveAs()
  359. {
  360.     SFReplyRec2     myReply;
  361.     char            *wtitle, *pstr, *wtptr, **wthndl;
  362.     unsigned int    wtlen;
  363.     unsigned long   refCon;
  364.  
  365.     SFAllCaps(1);
  366.  
  367.     myReply.nameRefDesc = refIsNewHandle;
  368.     myReply.pathRefDesc = refIsNewHandle;
  369.  
  370.     wtitle = smartGetWTitle(FrontWindow());         /* Window title -- pascal string. */
  371.     wtlen = *wtitle;                                /* Length of title. */
  372.  
  373.     wtptr = *(wthndl = (char **)NewHandle((long)(wtlen + 2), userID, 0, NULL));
  374.         /* Handle large enough for C1Input string version of window title. */
  375.  
  376.     wtitle = smartGetWTitle(FrontWindow());
  377.         /* If the title was cloned by NewWindow2 call into a handle, then this handle
  378.         ** may have moved due to the NewHandle above.  Dereference it again. */
  379.  
  380.     BlockMove(wtitle + 1, wtptr + 2, (long)wtlen);  /* Move characters into handle. */
  381.     *(int *)wtptr = wtlen;                          /* Set word length for C1Input string. */
  382.     HLock(wthndl);                                  /* Make it safe. */
  383.  
  384.     SFPutFile2(
  385.         180, 35,
  386.         refIsPointer,
  387.         "\pGive it a name, any name.",
  388.         refIsPointer,
  389.         wtptr,
  390.         &myReply
  391.     );
  392.     DisposeHandle(wthndl);
  393.  
  394.     if (myReply.good) {
  395.         if (refCon = GetWRefCon(FrontWindow())) DisposeHandle(refCon);
  396.             /* Get rid of old refCon handle. */
  397.  
  398.         SetWRefCon(myReply.pathRef, FrontWindow());
  399.             /* Set the refCon to the new handle. */
  400.  
  401.         pstr = *(char **)myReply.nameRef;   /* Convert C1Output string to pascal string. */
  402.         pstr[3] = pstr[2];
  403.         pstr += 3;
  404.         SetWTitle(pstr, FrontWindow());     /* Set the window title to new name. */
  405.         DisposeHandle(myReply.nameRef);
  406.  
  407.         putWindowIntoFile(myReply.pathRef);
  408.     }
  409. }
  410.  
  411.  
  412.  
  413. /******************************************************************************
  414. *
  415. * doSave
  416. *
  417. * This routine either saves the file (unless it is new then it does a save 
  418. * as).
  419. *
  420. ******************************************************************************/
  421. void    doSave()
  422. {
  423.     GrafPortPtr     wptr;
  424.     unsigned long   refCon;
  425.  
  426.     refCon = GetWRefCon(wptr = FrontWindow());
  427.  
  428.     if (!refCon) doSaveAs();
  429.     else {
  430.         putWindowIntoFile(refCon);
  431.     }
  432. }
  433.  
  434.  
  435.  
  436. /******************************************************************************
  437. *
  438. * NewWindow
  439. *
  440. * This routine opens a new untitled window.
  441. *
  442. ******************************************************************************/
  443. void    doNewWindow()
  444. {
  445.     GrafPortPtr     wptr;
  446.  
  447.     wptr = NewWindow2(
  448.         "\pUntitled",           /* Title reference*/
  449.         NULL,                   /* Ref con */
  450.         drawThisWindow,         /* Draw routine */
  451.         NULL,                   /* DefProc pointer */
  452.         refIsResource,          /* Param Table Descriptor */
  453.         MainWindowID,           /* Param Table Reference */
  454.         rWindParam1             /* Param Table Type */
  455.     );
  456.         
  457.     placeAndShowWindow(wptr);
  458. }
  459.  
  460.  
  461.  
  462. /*******************************************************************************
  463. *
  464. * SetUpWindows
  465. *
  466. * Sets up WindowList record for use through out the program.
  467. *
  468. *******************************************************************************/
  469. void    setupWindows()
  470. {
  471.     doNewWindow();
  472. }
  473.